package com.sixbro.authorization.security.service;

import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import com.sixbro.authorization.model.Principal;
import com.sixbro.authorization.model.Privilege;
import com.sixbro.authorization.security.component.MethodGrantedAuthority;
import com.sixbro.authorization.security.domain.OAuth2UserDetails;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author: Mr.Lu
 * @since: 2021/6/15 17:20
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    private final String[] roles = new String[]{"user:select","user:remove"};
    Map<String, Principal> allUser = new HashMap();

    @Autowired
    private PasswordEncoder passwordEncoder;


    @PostConstruct
    public void init() {
        allUser.put("admin", new Principal(1L, "admin", "123456", "18922206218", "1000"));
        allUser.put("root", new Principal(2L, "root", "123456", "18922206218", "1000"));
        allUser.put("guest", new Principal(3L, "guest", "123456", "18922206218", "1000"));
        allUser.put("test", new Principal(4L, "test", "123456", "18922206218", "1000"));
    }

    /**
     * 用户密码登录
     * @param username 用户名
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return builderUserDetails(username);
    }

    /**
     * 构建 userdetails
     * @return
     */
    private UserDetails builderUserDetails(String username){
        Principal principal = allUser.get(username);
        if (principal == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        List<Privilege> privileges = new ArrayList<>();
        privileges.add(new Privilege(100L, "yonghulibiao", "system:user:list", "/api/user/list", HttpMethod.GET));
        privileges.add(new Privilege(110L, "yonghushanchu", "system:user:remove", "/api/uaa/oauth/current/user", HttpMethod.GET));
        privileges.add(new Privilege(110L, "yonghushanchu", "system:user:remove", "/api/uaa/oauth/rsa/public-key", HttpMethod.GET));
        List<GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
        if (CollectionUtils.isNotEmpty(privileges)) {
//            Set<String> collect = permissions.stream().map((sysPermission -> String.format("[%s]%s", sysPermission.getMethod().name(), sysPermission.getUrl()))).collect(Collectors.toSet());
//            authorities = AuthorityUtils.createAuthorityList(collect.toArray(new String[0]));
            authorities = privileges.stream().map(sysPermission -> new MethodGrantedAuthority(sysPermission.getMethod().name(), sysPermission.getUrl())).collect(Collectors.toList());
        }
        String password = passwordEncoder.encode("123456");
        return new OAuth2UserDetails(principal.getId(), principal.getMobile(), principal.getTenantId(), principal.getUsername(), password, authorities);
    }
}
